package edu.rpi.testproject;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.sax.Element;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import org.apache.http.*;

public class Window2  extends Activity {
	Boolean play_status= true;
	Boolean gps_status = true;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main2);
        
        final ImageButton play_button = (ImageButton)findViewById(R.id.play_status);
    
        play_button.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View view) {
        		if(play_status){
        			play_button.setImageResource(R.drawable.pausebutton);
        			play_status=false;
        		}
        		else{
        			play_button.setImageResource(R.drawable.playbutton);
        			play_status=true;
        		}
        	}      
        });
        
        
        final ImageButton gps_button = (ImageButton)findViewById(R.id.gps_status);
        
        gps_button.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View view) {
        		//final TextView bottom= (TextView) findViewById(R.string.bottom);
        		//bottom.setText(play_button.getId());
        		if(gps_status){
        			gps_button.setImageResource(R.drawable.gps2);
        			gps_status=false;
        		}
        		else{
        			gps_button.setImageResource(R.drawable.gps);
        			gps_status=true;
        		}
        		//setResult(RESULT_OK);
        		//finish();
        	}      
        });
    }
    
    private void DownloadRSS(String URL)
    {
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            Document doc = null;
            DocumentBuilderFactory dbf = 
                DocumentBuilderFactory.newInstance();
            DocumentBuilder db;
            
            try {
                db = dbf.newDocumentBuilder();
                doc = db.parse(in);
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }        
            
            doc.getDocumentElement().normalize(); 
            
            //---retrieve all the <item> nodes---
            NodeList itemNodes = doc.getElementsByTagName("item"); 
            
            String strTitle = "";
            for (int i = 0; i < itemNodes.getLength(); i++) { 
                Node itemNode = itemNodes.item(i); 
                if (itemNode.getNodeType() == Node.ELEMENT_NODE) 
                {            
                    //---convert the Node into an Element---
                    Element itemElement = (Element) itemNode;
                    
                    //---get all the <title> element under the <item> 
                    // element---
                    NodeList titleNodes = 
                        ((Document) itemElement).getElementsByTagName("title");
                    
                    //---convert a Node into an Element---
                    Element titleElement = (Element) titleNodes.item(0);
                    
                    //---get all the child nodes under the <title> element---
                    NodeList textNodes = 
                        ((Node) titleElement).getChildNodes();
                    
                    //---retrieve the text of the <title> element---
                    strTitle = ((Node) textNodes.item(0)).getNodeValue();
                   
                    //---display the title---
                    Toast.makeText(getBaseContext(),strTitle, 
                        Toast.LENGTH_SHORT).show();
                } 
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();            
        }
    }
    
	 // Called only the first time the options menu is displayed.
	 // Create the menu entries.
	 // Menu adds items in the order shown.
    @Override
	 public boolean onCreateOptionsMenu(Menu menu) {
	     super.onCreateOptionsMenu(menu);
	     
	     // Parameters for menu.add are:
	     // group -- Not used here.
	     // id -- Used only when you want to handle and identify the click yourself.
	     // title
	     menu.add(0, 0, 0, "Favorites");
	     menu.add(0, 1, 0, "View");
	     menu.add(0, 2, 0, "Feed");
	     return true;
	 }

	 // Activity callback that lets your handle the selection in the class.
	 // Return true to indicate that you've got it, false to indicate
	 // that it should be handled by a declared handler object for that
	 // item (handler objects are discouraged for reasons of efficiency).
	 @Override
	 public boolean onOptionsItemSelected(MenuItem item){
	     switch (item.getItemId()) {
	     case 0:
	    	 Intent i = new Intent(Window2.this, Window1.class);
		    	startActivity(i);
	        // alert.setMessage("Test");
	         return true;
	     case 1:
	    	//Intent i = new Intent(Window2.this, testproject.class);
	    	//startActivity(i);
	        // showAlert("Menu Item Clicked", "Settings", "ok", null, false, null);
	         return true;
	     case 2:
	         Intent j = new Intent(Window2.this, Window3.class);
	         startActivity(j);
	         //showAlert("Menu Item Clicked", "Other", "ok", null, false, null);
	         return true;
	     }
	     return false;
	 }
}
